package com.hg.library.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;

import java.util.Random;

/**
 * Created by admin on 2017/3/1.
 */

public class MoreButton extends Button {
    private static final String TAG = "MoreButton";

    Paint mPaint = new Paint();

    private float padding = 10;
    Random random = new Random();

    public MoreButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        setBackground(null);
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                click();
            }
        });
    }

    private boolean isStart;

    private float angle = 360;

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth(3);
        mPaint.setColor(Color.argb(255, 65, 72, 75));

        if (isStart){
            angle += 10;
            if (angle >= 360){
                angle = 360;
                isStart = false;
            }
            invalidate();
        }

        Path path = new Path();

        RectF rectF = new RectF(padding, getHeight() / 2 - padding / 2, padding * 2, getHeight() / 2 + padding / 2);
        path.addArc(rectF, angle, angle);
        canvas.drawPath(path, mPaint);

        rectF = new RectF(getWidth() / 2 - padding / 2, getHeight() / 2 - padding / 2, getWidth() / 2 + padding / 2, getHeight() / 2 + padding / 2);
        path.addArc(rectF, angle, angle);
        canvas.drawPath(path, mPaint);

        rectF = new RectF(getWidth() - padding * 2, getHeight() / 2 - padding / 2,getWidth() - padding, getHeight() / 2 + padding / 2);
        path.addArc(rectF,angle,angle);
        canvas.drawPath(path,mPaint);
    }

    public void click(){
        if (!isStart) {
            isStart = true;
            angle = 0;
            invalidate();
        }
    }
}
